package com.learn.egcar1.base.interceptors;

import com.learn.egcar1.base.response.JsonResult;
import com.learn.egcar1.base.response.StatusCode;
import com.learn.egcar1.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("进入登录校验的拦截器");
        /*
        * 1.校验用户登录状态
        * 2.登录状态，重定向到登录页面login。html
        *   登录状态，return true
        * */
        HttpSession session = request.getSession();
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO==null){
            response.sendRedirect("/login.html");
            return false;
        }

        return true;
    }
}
